package com.electric.action;

import java.util.List;
import java.util.Map;

import com.electric.base.BaseAction;
import com.electric.model.User;
import com.electric.service.UserService;
import com.electric.util.CookieUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author Kevin Zhang
 *
 * 2011-5-10
 */
public class LoginAction extends BaseAction {
	
	private UserService userService = new UserService();
	// 用户Ajax返回数据   
    private String result;
    
    private String email;
    private String pass;
	@Override
	public String execute() throws Exception {
		List<User> users = userService.verifyEmailAndPass(email, pass);
		if(users.size() > 0){
			//1.把用户存进session和cookie中
			Map session = ActionContext.getContext().getSession();
			session.put("userName",users.get(0).getUsername());
			CookieUtil.addCookie("userName", users.get(0).getUsername());
			//2.把用户信息返回
			setResult(users.get(0).getUsername());
		}
		else{
			setResult("ERROR");
		}
		
		return Action.SUCCESS;
	}
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPass() {
		return pass;
	}
	public void setPass(String pass) {
		this.pass = pass;
	}
}
